<?php
/**
 * *[LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-01 22:12:12;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */
namespace application\core;

class Date
{

    /**
     * 获取当前时间戳毫秒数
     * @return string
     */
    public static function getMs(): string
    {
        list ($uses, $sec) = explode(" ", microtime());
        return sprintf('%.0f', ((float) $uses + (float) $sec) * 1000);
    }

    /**
     * @param $time
     * @return string
     */
    public static function dateToMs($time): string
    {
        list ($uses, $sec) = explode(".", $time);
        $date = strtotime($uses);
        // 不足13位。右边补0
        $sec = str_pad($sec, 3, "0", STR_PAD_LEFT);
        return $date . $sec;
    }

    /**
     * 格式化时间戳，精确到毫秒，x代表毫秒
     * @param $tag
     * @param $time
     * @return array|false|string|string[]
     */
    public static function uDate($tag, $time)
    {
        $dateArr = explode(".", $time / 1000);
        $uses = $dateArr[0];
        $sec = $dateArr[1] ?? 0;
        $date = date($tag, $uses);
        return str_replace('x', $sec, $date);
    }

    /**
     * 计算两个日期相差的月数
     * @param $date1
     * @param $date2
     * @return float|int
     */
    public static function getDiffMonthNum($date1, $date2)
    {
        $date1_stamp = strtotime($date1);
        $date2_stamp = strtotime($date2);
        list ($date_1['y'], $date_1['m']) = explode("-", date('Y-m', $date1_stamp));
        list ($date_2['y'], $date_2['m']) = explode("-", date('Y-m', $date2_stamp));
        return abs(($date_2['y'] - $date_1['y']) * 12 + $date_2['m'] - $date_1['m']);
    }

    /**
     * 计算两个日期相差的天数
     * @param $day1
     * @param $day2
     * @return float|int
     */
    public static function diffBetweenTwoDays($day1, $day2)
    {
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);
        if ($second1 < $second2) {
            $tmp = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        return ($second1 - $second2) / 86400;
    }

    /**
     * 获取星期
     * @param string $week
     * @return string
     */
    public static function getWeek(string $week = ''): string
    {
        if ($week == "1") {
            return "星期一";
        } else if ($week == "2") {
            return "星期二";
        } else if ($week == "3") {
            return "星期三";
        } else if ($week == "4") {
            return "星期四";
        } else if ($week == "5") {
            return "星期五";
        } else if ($week == "6") {
            return "星期六";
        } else if ($week == "0") {
            return "星期日";
        } else {
            return "";
        }
    }

    /**
     * 获取某个月最大天数
     * @param $year
     * @param $month
     * @return int
     */
    public static function getMonthLastDay($year, $month): int
    {
        if ($year == '') {
            $year = date("Y", time());
        }
        switch ($month) {
            case 4:
            case 6:
            case 9:
            case 11:
                $days = 30;
                break;
            case 2:
                if ($year % 4 == 0) {
                    if ($year % 100 == 0) {
                        $days = $year % 400 == 0 ? 29 : 28;
                    } else {
                        $days = 29;
                    }
                } else {
                    $days = 28;
                }
                break;
            default:
                $days = 31;
                break;
        }
        return $days;
    }
}

